﻿/*
This is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
function DNJDebug(msg)
{  
    if (window.console && window.console.warn) console.warn("DNJ DBG :: %s", msg);
    else alert(msg);
}


(function (jQuery){
    jQuery.__DNJRPC__ = function(options){
    var settings =
        {
            target:null,
            success: null            
        };        
        
    //ensure call from main instance    
    var dnjObj = window.__dnjObj || this;
    window.__dnjObj = window.__dnjObj || dnjObj;
    //get request counter back or initialize it
    dnjObj.jsoncount = dnjObj.jsoncount || 1;
    
    
    //we need current settings
    if(options) { $.extend(settings, options);}
    
    $(document).data(dnjObj.jsoncount+'', options);
       
    function SuccessCB(data){
        options = $(document).data(data.id+'');
        
        
        if (typeof(options.success) == 'function')
            options.success(data);
        if (options.target != null)
        {
            trg = $(options.target);
            if (typeof(trg.html) == 'function')
                trg.html(data.result);
            if (typeof(trg.val) == 'function')
                trg.val(data.result);
               
            //settings.target = null;
        }                                   
    };
    function DNJCall(function_name, _arguments)
    {             
        var jsondata = {method:function_name, params:new Array(), id:dnjObj.jsoncount};
        var curId = dnjObj.jsoncount + '';
        dnjObj.jsoncount++;
        
        var args='';       
        for (var i = 0; i < _arguments.length; i++)
            jsondata.params.push(_arguments[i]);
              
        //$.post('DNJUndefinedInvoker', jsondata, SuccessCB, 'json');                   
         $.ajax({
           type: 'POST',
           url: 'DNJUndefinedInvoker',
           data: jsondata,
           success: SuccessCB,
           dataType : 'json',
           complete : function() {$(document).removeData(curId);}
         });
        
        
    };
    function RPCFactory(name, Pack)   
    {
        var e=dnjObj;
        var w=$;
        Pack = 'DNJRPC.'+Pack;
        var ns=Pack.split(".");       
        for(var i=0;i<ns.length;i++)
        {
            if (i>0)
            {
                e[ns[i]] = e[ns[i]] || {};
                e=e[ns[i]];
            }
           
            w[ns[i]] = w[ns[i]] || {};
            w=w[ns[i]];
        }
        w[name] = e[name] = function() {DNJCall(name, arguments)};
    }
    
    $['DNJRPC']['RPCFactory'] = RPCFactory;
    return jQuery;}
})(jQuery);

//dnj json rpc object constructor
$.DNJRPC = function()
{
    t = arguments[0];
    if (typeof(t) == 'string')
        return $.__DNJRPC__({target:t});
       
    if (typeof(t) == 'object')
        return $.__DNJRPC__(t);
   
    return $.__DNJRPC__();
};

//create initial instance
$.DNJRPC();    
